package com.example.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/users")
public class testRest {

    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @PostMapping
    public String test1(){
        System.out.println("save user......");
        return "save user......";
    }
    @DeleteMapping
    public String test2(){
        System.out.println("DELETE user......");
        return "DELETE user......";
    }
    @PutMapping
    public String test3(){
        System.out.println("update user......");
        return "update user......";
    }
    @GetMapping
    public String test4(){
        System.out.println("getAll user......");
        return "getAll user......";
    }
    @GetMapping("/{id}")
    public String test5(@PathVariable int id){
        System.out.println("getOne user id="+id);
        return "getOne user id="+id;
    }
    @PostMapping("redis/add/{name}")
    public String redisAdd(@PathVariable String name){
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set("myname",name);
        return "已成添加 myname:"+name;
    }
    @GetMapping("redis/get/{key}")
    public String redisGet(@PathVariable String key){
        ValueOperations valueOperations = redisTemplate.opsForValue();
        Object value = valueOperations.get(key);
        return "获取数据 myname:"+value;
    }
    @PostMapping("redis/addByStrTem/{age}")
    public String redisAddByStrTem(@PathVariable String age){
        ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
        valueOperations.set("age",age);
        return "已添加 age:"+age;
    }
    @GetMapping("redis/getByStrTem/{key}")
    public String redisGetByStrTem(@PathVariable String key){
        ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
        Object value = valueOperations.get(key);
        return "获取数据 age:"+value;
    }
}
